package tt.dz.service.imp.middleware;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.springframework.stereotype.Component;
import tt.dz.dao.ext.system.OmsOperationSpListDao;
import tt.dz.entity.exception.BizException;
import tt.dz.entity.system.OmsOperationSpList;
import tt.dz.util.StringUtil;
import tt.dz.util.serialGenerater;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/15.
 */
@Component
public class Ev3c2MessageWrapperImpl implements MessageWrapper
{
	private Log log = LogFactory.getLog(MessageWrapper.class);

	@Resource
	OmsOperationSpListDao omsOperationSpListDao;

	@Override
	public String wrapSendingMsg(String body, String frameCode, String chargerId, String chargerType, String portNo, String encryption)
	{
		String sendMsgHeader, msg;
		//生成报文头
		String beginChar = "68";
		String msgBodylenBin = StringUtil.ev3cMsgBodyBin(body);
		String oms_msg_charge_jrnl = serialGenerater.generateSerialNo();
		String communicationNo = null;//默认为电庄的通讯编号
		//查询  充电运营服务商表 原名叫oms_oper_merchant，新名称叫oms_operation_splist
		//		String condition = " where oper_merchant_id in ( select oper_merchant_id from oms_station where " + " station_id = (select station_id from oms_charger where charger_id = '" + chargerId + "' ))";

		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT s.communication_no ");
		sql.append(" FROM oms_charger c ");
		sql.append(" INNER JOIN oms_operation_sp_list s ON c.oper_merchant_id = s.oper_merchant_id ");
		sql.append(" WHERE c.charger_id = ? ");

		List<Map<String, Object>> list = omsOperationSpListDao.findDataBySql(sql.toString(), chargerId);
		if (list != null && !list.isEmpty())
		{
			communicationNo = (String) list.get(0).get("communication_no");
		}

		sendMsgHeader = beginChar + msgBodylenBin + frameCode + communicationNo + chargerId + convertChargerType(chargerType) + portNo + encryption + oms_msg_charge_jrnl;
		//生成整个报文
		msg = sendMsgHeader + body;
		if (sendMsgHeader.length() != 50)
		{
			log.error("oms-sendMsgHeader.length != 50");
			return null;
		}
		if (msg.contains("null"))
		{
			log.error("oms-sendMsg include null");
			return null;
		}
		return msg;
	}

	@Override
	public String wrapReplyMsg(String body, String frameCode, String chargerId, String chargerType, String portNo, String encryption, String replyCode, String chargerSerialNo)
	{
		String sendMsgHeader, msg;
		//生成报文头
		String beginChar = "68";
		String msgBodylenBin = StringUtil.ev3cMsgBodyBin(body);
		String oms_msg_charge_jrnl = serialGenerater.generateSerialNo();
		String communicationNo = null;//默认为电庄的通讯编号
		//查询  充电运营服务商表 原名叫oms_oper_merchant，新名称叫oms_operation_splist
		//		String condition = " where oper_merchant_id in ( select oper_merchant_id from oms_station where " + " station_id = (select station_id from oms_charger where charger_id = '" + chargerId + "' ))";

		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT s.communication_no ");
		sql.append(" FROM oms_charger c ");
		sql.append(" INNER JOIN oms_operation_sp_list s ON c.oper_merchant_id = s.oper_merchant_id ");
		sql.append(" WHERE c.charger_id = ? ");

		List<Map<String, Object>> list = omsOperationSpListDao.findDataBySql(sql.toString(), chargerId);
		if (list != null && !list.isEmpty())
		{
			communicationNo = (String) list.get(0).get("communication_no");
		}

		sendMsgHeader = beginChar + msgBodylenBin + frameCode + communicationNo + chargerId + chargerType + portNo + replyCode + encryption + chargerSerialNo + oms_msg_charge_jrnl;
		//生成整个报文
		msg = sendMsgHeader + body;
		if (sendMsgHeader.length() != 74)
		{
			log.error("oms-sendMsgHeader.length != 50");
			return null;
		}
		if (msg.contains("null"))
		{
			log.error("oms-sendMsg include null");
			return null;
		}
		return msg;
	}

	private String convertChargerType(String chargerType)
	{
		if ("2".equals(chargerType))
			return "0";
		else
			return chargerType;
	}
}
